<?php defined('InNcPHP') or exit('Access Invalid!');?>

<style>
    .show { display: block; }
    .hide { display: none; }
</style>

<script type="text/javascript" src="<?php echo RESOURCE_URL;?>/common/jquery-ui/jquery.ui.js"></script>
<script type="text/javascript" src="<?php echo RESOURCE_URL;?>/common/jquery-ui/i18n/zh-CN.js" charset="utf-8"></script>
<link rel="stylesheet" type="text/css" href="<?php echo RESOURCE_URL;?>/common/jquery-ui/themes/ui-lightness/jquery.ui.css"  />

<div class="page">
    <div class="fixed-bar">
        <div class="item-title">
            <h3><?php echo $lang['ticket_index_ticket'];?></h3>
            <?php echo $output['top_link'];?>
        </div>
    </div>
    <div class="fixed-empty"></div>
    <!--  当前操作的门票  -->
    <blockquote style="border-left: 5px solid #ccc;background-color:#f1f1f1;padding: 10px 15px;margin: 10px 0;">
        <?php echo $lang['ticket_index_title']; ?>：<?php echo $output['ticket']['tk_title']; ?><br>
        <?php echo $lang['ticket_add_show_price']; ?>：<?php echo $lang['currency'].$output['ticket']['tk_show_price']; ?>
    </blockquote>
    <!--  新增按钮  -->
    <a href="JavaScript:void(0);" class="btn" id="addBtn"><span><?php echo $lang['nc_new'];?></span></a>
    <!--  列表  -->
    <form method="post" id='form_days'>
        <input type="hidden" name="form_submit" value="ok" />
        <table class="table tb-type2">
            <thead>
            <tr class="space">
                <th colspan="15" class="nobg"><?php echo $lang['nc_list'];?></th>
            </tr>
            <tr class="thead">
                <th><?php echo $lang['nc_common_select']; ?></th>
                <th class="align-center"><?php echo $lang['ticket_day_date'];?></th>
                <th>SKU</th>
                <th class="align-center"><?php echo $lang['nc_handle'];?></th>
            </tr>
            </thead>
            <tbody>
            <?php if(!empty($output['list']) && is_array($output['list'])){ ?>
                <?php foreach($output['list'] as $k => $v){ ?>
                    <tr class="hover">
                        <td class="w24">
                            <input name="del_id[]" type="checkbox" value="<?php echo $v['date_id']; ?>" class="checkitem">
                        </td>
                        <td class="align-center"><?php echo $v['date'];?></td>
                        <td>
                            <?php foreach ($v['sku'] as $sku): ?>
                                <p><?php echo $sku['spec_name'].'/'.''; ?><?php echo $lang['ticket_index_store'].':'.$sku['sku_store'].'/'.$lang['ticket_index_price'].':'.$lang['currency'].$sku['sku_price'].'/'.'商家成本价：'.$lang['currency'].$sku['sku_cost_price'] ?></p>
                            <?php endforeach; ?>
                        </td>
                        <td class="w150 align-center">
                            <a href="javascript:;" onclick="editDay(<?php echo $v['date_id']; ?>,'<?php echo $v['date'];?>');"><?php echo $lang['nc_edit'];?></a> |
                            <a href="javascript:;" onclick="delDay(<?php echo $v['date_id']; ?>, '<?php echo $v['date'];?>');"><?php echo $lang['nc_delete'];?></a>
                        </td>
                    </tr>
                <?php } ?>
            <?php }else { ?>
                <tr class="no_data">
                    <td colspan="10"><?php echo $lang['nc_no_record'];?></td>
                </tr>
            <?php } ?>
            </tbody>
            <tfoot>
            <?php if(!empty($output['list']) && is_array($output['list'])){ ?>
                <tr class="tfoot">
                    <td><input type="checkbox" class="checkall" id="checkallBottom" name="chkVal"></td>
                    <td colspan="16">
                        <label for="checkallBottom"><?php echo $lang['nc_select_all']; ?></label>
                        <a href="JavaScript:void(0);" class="btn" id="delAll"><span><?php echo $lang['nc_del'];?></span></a>
                        <div class="pagination"> <?php echo $output['page'];?> </div>
                    </td>
                </tr>
            <?php } ?>
            </tfoot>
        </table>
    </form>

    <!-- 添加表单 -->
    <form id="days_add" method="post" class="hide">
        <input type="hidden" name="tk_id" value="<?php echo $output['ticket']['tk_id']; ?>">
        <table class="table tb-type2">
            <tbody>
            <!-- 日期 -->
            <tr>
                <td colspan="2" class="required"><label class="validation"><?php echo $lang['ticket_day_date'];?>:</label></td>
            </tr>
            <tr class="noborder">
                <td class="vatop rowform"><input type="text" id="date" name="date" class="txt date"></td>
                <td class="vatop tips"></td>
            </tr>

            <!-- SKU设置 -->
            <?php foreach ($output['spec_list'] as $type): ?>
                <tr class="noborder type_fix">
                    <td colspan="2" class="required">
                        <label><input type="checkbox" name="types[<?php echo $type['spec_id'];?>]" onclick="changeUse($(this));"><?php echo $type['spec_name'];?></label>
                    </td>
                </tr>
            <tr class="type_fix inputs hide">
                <td colspan="2">
                <?php foreach($output['types'] as $type2){ ?>
                        <div>
                         <?php echo $type2['type_name'].'|'.$lang['ticket_index_store']; ?>：<input type="text" name="store[<?php echo $type2['type_id'];?>][<?php echo $type['spec_id'];?>]" style="width:15%;">
                        <?php echo $lang['ticket_index_price']; ?>：<input type="text" name="price[<?php echo $type2['type_id'];?>][<?php echo $type['spec_id'];?>]" <?php if($output['admin_info']['merchant_id']>0){ ?> readonly="true" <?php } ?> style="width:15%;">
                        <?php echo '协议价'; ?>：<input type="text" name="cost_price[<?php echo $type2['type_id'];?>][<?php echo $type['spec_id'];?>]" style="width:15%;" <?php if($output['admin_info']['merchant_id']>0){ ?> readonly="true" <?php } ?>>
                        </div>
               <?php  }?>
                </td>
            </tr>

                <!--<tr class="type_fix inputs hide">
                    <td colspan="2">
                        <?php /*echo $lang['ticket_index_store']; */?>：<input type="text" name="store<?php /*echo $type['spec_id'];*/?>">
                        <?php /*echo $lang['ticket_index_price']; */?>：<input type="text" name="price<?php /*echo $type['spec_id'];*/?>">
                    </td>
                </tr>-->
            <?php endforeach; ?>

            </tbody>
            <tfoot>
            <tr>
                <td colspan="2"><a href="JavaScript:void(0);" class="btn" id="addSubmit"><span><?php echo $lang['nc_add'];?></span></a></td>
            </tr>
            </tfoot>
        </table>
    </form>

    <!-- 编辑表单 -->
    <form id="days_edit" method="post" class="hide">
        <input type="hidden" name="date_id">
        <table class="table tb-type2">
            <tbody>
            <!-- 日期 -->
            <tr>
                <td colspan="2" class="required"><label class="validation"><?php echo $lang['ticket_day_date'];?>:</label></td>
            </tr>
            <tr class="noborder">
                <td class="vatop rowform"><input type="text" disabled name="date" class="txt date"></td>
                <td class="vatop tips"></td>
            </tr>

            <!-- SKU设置 -->
            <?php foreach ($output['spec_list'] as $type): ?>
                <tr class="noborder type_fix">
                    <td colspan="2" class="required">
                        <label><input type="checkbox" name="types[<?php echo $type['spec_id'];?>]" onclick="changeUse($(this));"><?php echo $type['spec_name'];?></label>
                    </td>
                </tr>
                <tr class="type_fix inputs hide">
                    <td colspan="2">
                        <?php foreach($output['types'] as $type2){ ?>
                            <p>
                                <?php echo $type2['type_name'].'|'.$lang['ticket_index_store']; ?>：<input type="text" name="store[<?php echo $type2['type_id'];?>][<?php echo $type['spec_id'];?>]" style="width:15%;">
                                <?php echo $lang['ticket_index_price']; ?>：<input type="text" name="price[<?php echo $type2['type_id'];?>][<?php echo $type['spec_id'];?>]" style="width:15%;" <?php if($output['admin_info']['merchant_id']>0){ ?> readonly="true" <?php } ?>  >
                                <?php echo '协议价'; ?>：<input type="text" <?php if($output['admin_info']['merchant_id']>0){ ?> readonly="true" <?php } ?> name="cost_price[<?php echo $type2['type_id'];?>][<?php echo $type['spec_id'];?>]" style="width:15%;">
                            </p>
                        <?php  }?>
                    </td>
                </tr>
                <!--<tr class="type_fix inputs hide">
                    <td colspan="2">
                        <?php /*echo $lang['ticket_index_store']; */?>：<input type="text" name="store<?php /*echo $type['spec_id'];*/?>">
                        <?php /*echo $lang['ticket_index_price']; */?>：<input type="text" name="price<?php /*echo $type['spec_id'];*/?>">
                    </td>
                </tr>-->
            <?php endforeach; ?>

            </tbody>
            <tfoot>
            <tr>
                <td colspan="2"><a href="JavaScript:void(0);" class="btn" id="editSubmit"><span><?php echo $lang['nc_update'];?></span></a></td>
            </tr>
            </tfoot>
        </table>
    </form>
</div>

<script>
    //ajax请求登录过期重定向
    $(document).ajaxError(function(event,request, settings){
        location.reload();
    });

    //门票类型改变选中状态（儿童票、成人票等）
    function changeUse(obj) {
        var td = obj.parent().parent().parent().next();
        if (obj.is(':checked') == false) {
            td.hide().find('input').val('');
        } else {
            td.show();
        }
    }

    //删除一条数据
    function delDay(id, date) {
        layer.confirm('<?php echo $lang['ticket_day_confirm_del']; ?>' + date + '？', {
            btn: ['<?php echo $lang['nc_yes']; ?>', '<?php echo $lang['nc_no']; ?>']
        }, function (index) {
            $.post('index.php?act=ticket&op=days_ajax&type=del', {date_id: id}, function (data) {
                if (data.res) {
                    layer.alert('<?php echo $lang['ticket_day_del_succ']; ?>', function () {
                        location.reload();
                    });
                } else {
                    layer.alert('<?php echo $lang['ticket_day_del_fail']; ?>');
                }
            }, 'json');
        });
    }
    //批量删除
    $('#delAll').click(function () {
        var inputs = $('input[name="del_id[]"]:checked')
        var ids = [];
        inputs.each(function (i) {
            ids.push(this.value);
        });
        //校验选项是否为空
        if (ids.length < 1) {
            layer.alert('<?php echo $lang['ticket_index_choose_null']; ?>');
            return;
        }
        //删除所有选择的日期
        layer.confirm('<?php echo $lang['ticket_day_confirm_del']; ?>', {
            btn: ['<?php echo $lang['nc_yes']; ?>', '<?php echo $lang['nc_no']; ?>']
        }, function (index) {
            $.post('index.php?act=ticket&op=days_ajax&type=del_all', {ids: ids}, function (data) {
                if (data.res) {
                    layer.alert('<?php echo $lang['ticket_day_del_succ']; ?>', function () {
                        location.reload();
                    });
                } else {
                    layer.alert('<?php echo $lang['ticket_day_del_fail']; ?>');
                }
            }, 'json');
        });
    });




    //编辑的表单
    var editForm = $('#days_edit');
    //编辑
    function editDay(id, date) {
        $.post('index.php?act=ticket&op=days_ajax&type=info', {date_id: id}, function (data) {
            editForm.find('input[name="date_id"]').val(data.date_id);
            editForm.find('input[name="date"]').val(data.date);
            for (var i in data.sku) {
                editForm.find('input[name="types[' + data.sku[i].spec_id + ']"]').attr('checked', true);
                editForm.find('input[name="store[' + data.sku[i].type_id + '][' + data.sku[i].spec_id + ']"]').val(data.sku[i].sku_store).parent().parent().parent().show();
                editForm.find('input[name="price[' + data.sku[i].type_id + '][' + data.sku[i].spec_id + ']"]').val(data.sku[i].sku_price);
                editForm.find('input[name="cost_price[' + data.sku[i].type_id + '][' + data.sku[i].spec_id + ']"]').val(data.sku[i].sku_cost_price);
            }
            layer.open({
                type: 1,
                title: '<?php echo $lang['nc_edit'];?>' + date + ',暂无的票型请留空！！！',
                area: ['700px', '410px'],
                content: editForm,
                cancel: function () {
                    $('label.error').hide();
                    editForm.find('input.error').removeClass('error');
                }
            });
        }, 'json');
    }
    //提交编辑表单
    $("#editSubmit").click(function(){
        if (editForm.valid()) {
            if (editForm.find('input[name^="types"]:checked').length === 0) {
                layer.alert('<?php echo $lang['ticket_add_use_null']; ?>');
            } else {
                $.post('index.php?act=ticket&op=days_ajax&type=update', editForm.serialize(), function (data) {
                    if (data.res) {
                        layer.alert('<?php echo $lang['nc_common_update_succ']; ?>', function () {
                            location.reload();
                        });
                    } else {
                        layer.alert('<?php echo $lang['nc_common_update_fail']; ?>');
                    }
                }, 'json');
            }
        }
    });
    //编辑表单校验
    editForm.validate({
        errorPlacement: function(error, element){
            error.appendTo(element.parent().parent().parent());
        },
        rules: {
           /* <?php foreach ($output['spec_list'] as $type): ?>
            <?php foreach ($output['types'] as $type2): ?>
            "store[<?php echo $type2['type_id'];?>][<?php echo $type['spec_id'];?>]": {
                required: function () {
                    return editForm.find('input[name="types[<?php echo $type['spec_id']; ?>]"]').is(':checked')
                },
                digits: true,
                min: 1,
                max: 9999999999
            },
            <?php if($output['admin_info']['merchant_id']==0): ?>
            "price[<?php echo $type2['type_id'];?>][<?php echo $type['spec_id'];?>]": {
                required: function () {
                    return editForm.find('input[name="types[<?php echo $type['spec_id']; ?>]"]').is(':checked')
                },
                number: true,
                min: 0.01,
                max: 9999999
            },

            "cost_price[<?php echo $type2['type_id'];?>][<?php echo $type['spec_id'];?>]": {
                required: function () {
                    return addForm.find('input[name="types[<?php echo $type['spec_id']; ?>]"]').is(':checked')
                },
                number: true,
                min: 0.01,
                max: 9999999
            },
            <?php  endif;?>
            <?php  endforeach;?>
            <?php endforeach; ?>*/
        },
        messages : {
            <?php foreach ($output['spec_list'] as $type): ?>
            <?php foreach ($output['types'] as $type2): ?>
            "store[<?php echo $type2['type_id'];?>][<?php echo $type['spec_id'];?>]": '<?php echo $lang['ticket_add_store_notice']; ?>',
            "price[<?php echo $type2['type_id'];?>][<?php echo $type['spec_id'];?>]": '<?php echo $lang['ticket_add_price_notice']; ?>',
            <?php endforeach; ?>
            <?php endforeach; ?>
        }
    });



    //新增的表单
    var addForm = $('#days_add');
    //销售日期
    $('#date').datepicker({
        dateFormat: 'yy-mm-dd',
        minDate: 0
    });
    //点击新增按钮
    $('#addBtn').click(function () {
        layer.open({
            type: 1,
            title: '<?php echo $lang['nc_new'];?>'+',暂无的票券请留空！！！',
            area: ['700px', '410px'],
            content: $('#days_add'),
            cancel: function () {
                $('label.error').hide();
                addForm.find('input.error').removeClass('error');
            }
        });
    });
    //表单提交
    $('#addSubmit').click(function () {
        if (addForm.valid()) {

            if (addForm.find('input[name^="types"]:checked').length === 0) {
                layer.alert('<?php echo $lang['ticket_add_use_null']; ?>');
            } else {
                $.post('index.php?act=ticket&op=days_ajax&type=add', addForm.serialize(), function (data) {
                    if (data.res) {
                      layer.alert(data.msg, function () {
                            location.href = 'index.php?act=ticket&op=days&tk_id=<?php echo $output['ticket']['tk_id']; ?>';
                        });
                    } else {
                        layer.alert(data.msg);
                    }
                }, 'json');
            }
        }
    });
 /*   //新增表单校验
    addForm.validate({
        errorPlacement: function(error, element){
            error.appendTo(element.parent().parent().prev().find('td:first'));
        },
        rules: {
            date: 'required',
            <?php foreach ($output['types'] as $type): ?>
            store<?php echo $type['type_id']; ?>: {
                required: function () {
                    return addForm.find('input[name="types[<?php echo $type['type_id']; ?>]"]').is(':checked')
                },
                digits: true,
                min: 1,
                max: 9999999999
            },
            price<?php echo $type['type_id']; ?>: {
                required: function () {
                    return addForm.find('input[name="types[<?php echo $type['type_id']; ?>]"]').is(':checked')
                },
                number: true,
                min: 0.01,
                max: 9999999
            },
            <?php endforeach; ?>
        },
        messages : {
            date: '<?php echo $lang['ticket_day_date_null']; ?>',
            <?php foreach ($output['types'] as $type): ?>
            store<?php echo $type['type_id']; ?>: '<?php echo $lang['ticket_add_store_notice']; ?>',
            price<?php echo $type['type_id']; ?>: '<?php echo $lang['ticket_add_price_notice']; ?>',
            <?php endforeach; ?>
        }
    });*/
    //新增表单校验
    addForm.validate({
        errorPlacement: function(error, element){
            error.appendTo(element.parent().parent().parent());
        },
        rules: {
            date: 'required',
           /* <?php foreach ($output['spec_list'] as $type): ?>
            <?php foreach ($output['types'] as $type2): ?>
            "store[<?php echo $type2['type_id'];?>][<?php echo $type['spec_id'];?>]": {
                required: function () {
                    return addForm.find('input[name="types[<?php echo $type['spec_id']; ?>]"]').is(':checked')
                },
                digits: true,
                min: 1,
                max: 9999999999
            },
            <?php if($output['admin_info']['merchant_id']==0): ?>
            "price[<?php echo $type2['type_id'];?>][<?php echo $type['spec_id'];?>]": {
                required: function () {
                    return addForm.find('input[name="types[<?php echo $type['spec_id']; ?>]"]').is(':checked')
                },
                number: true,
                min: 0.01,
                max: 9999999
            },
            "cost_price[<?php echo $type2['type_id'];?>][<?php echo $type['spec_id'];?>]": {
                required: function () {
                    return addForm.find('input[name="types[<?php echo $type['spec_id']; ?>]"]').is(':checked')
                },
                number: true,
                min: 0.01,
                max: 9999999
            },
            <?php endif; ?>
            <?php  endforeach;?>
            <?php endforeach; ?>*/
        },
        messages : {
            date: '<?php echo $lang['ticket_day_date_null']; ?>',
            <?php foreach ($output['spec_list'] as $type): ?>
            <?php foreach ($output['types'] as $type2): ?>
            "store[<?php echo $type2['type_id'];?>][<?php echo $type['spec_id'];?>]": '库存不能为空',
            "price[<?php echo $type2['type_id'];?>][<?php echo $type['spec_id'];?>]": '售价不能为空',
            "cost_price[<?php echo $type2['type_id'];?>][<?php echo $type['spec_id'];?>]": '协议价不能为空',
            <?php endforeach; ?>
            <?php endforeach; ?>
        }
    });
</script>